<?php

// search area

// trades dropdown

$trades_dropdown = '<option value="">All trades</option>';

echo '<!-- '.$_GET['trade'].'-->';

$sql = "SELECT trade, url FROM trades ORDER BY trade";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
while($trades_rs=mysql_fetch_assoc($query)){
$active = (isset($_GET['trade']) && $_GET['trade'] == $trades_rs['url']) ? ' selected="selected"' : '' ;
$real_trade = (isset($_GET['trade']) && $_GET['trade'] == $trades_rs['url']) ? htmlentities($trades_rs['trade']) : $real_trade;
$trades_dropdown.='<option value="'.$trades_rs['url'].'"'.$active.'>'.htmlentities($trades_rs['trade']).'</option>'."\r\n";
}

// areas dropdown

$areas_dropdown = '<option value="">All areas</option>';

$sql = "SELECT area, url FROM areas ORDER BY area";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
while($areas_rs=mysql_fetch_assoc($query)){
$active = (isset($_GET['area']) && $_GET['area'] == $areas_rs['url']) ? ' selected="selected"' : '' ;
$real_area = (isset($_GET['area']) && $_GET['area'] == $areas_rs['url']) ? htmlentities($areas_rs['area']) : $real_area;
$areas_dropdown.='<option value="'.$areas_rs['url'].'"'.$active.'>'.htmlentities($areas_rs['area']).'</option>'."\r\n";
}

// radius dropdown

$i = 10;

while($i <= 100){
$active = ($i == $_GET['radius']) ? ' selected="selected"' : '' ;
$radius_dropdown.='<option value="'.$i.'"'.$active.'>'.$i.' miles</option>';
$i = $i + 10;
}

$tradesmen_search='

<aside id="search_panel">

<h1>Search tradesmen</h1>

<form method="get" action="/tradesmen">

<div class="rowWrapper">
<div class="rowLeft"><label for="trade">Trade:</label></div>
<div class="rowRight"><select name="trade" id="trade_search">
'.$trades_dropdown.'
</select></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="area">Area:</label></div>
<div class="rowRight"><select name="area" id="area">
'.$areas_dropdown.'
</select></div>
</div>

<!--<div class="rowWrapper">
<div class="rowLeft"><label for="postcode">Postcode:</label></div>
<div class="rowRight"><input type="text" name="postcode" id="postcode" value="'.urldecode($_GET['postcode']).'" class="textbox" style="width: 60px;" /></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="radius">Radius:</label></div>
<div class="rowRight"><select name="radius" id="radius">
'.$radius_dropdown.'
</select></div>
</div>-->

<script type="text/javascript">document.write(\'<\'+\'a onclick="return tradesmen_search();" style="float: right; cursor: pointer; margin-top: 5px;"><\'+\'img src="/images/search_button.gif" alt="Search" /><\'+\'/a>\');</script>
<noscript><input type="image" src="/images/search_button.gif" alt="search" style="float: right; cursor: pointer; margin-top: 5px;" /></noscript>

</form>

</aside>
';

// logged in?

$user_session = explode("_", $_SESSION['user']);

if(isset($_SESSION['user'])){

	$account_header = 'My account';

	// name and type

	$sql = "SELECT tradesman.id, firstname, username, company_name, sms_remaining FROM users 
		LEFT JOIN SMS ON users.id = SMS.user_id 
		LEFT JOIN tradesman ON users.id = tradesman.user_id 
		WHERE users.id = '".$user_session[0]."'";
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rs=mysql_fetch_assoc($query);

	$rs['sms_remaining'] = (strlen($rs['sms_remaining']) == 0) ? 0 : $rs['sms_remaining'] ;

	if($_SESSION['tradesman'] == '1'){
	
	$account_area = '<p style="margin: 0px 0px 10px 0px;"><strong>Welcome '.ucfirst($rs['firstname']).'</strong></p>

	<ul id="account_links">
	<li><a href="/my-account">My account</a></li>
	<!--<li><a href="/buy-sms">Buy SMS</a> ('.$rs['sms_remaining'].' remaining)</li>-->
	<li><a href="'.build_tradesman_link($rs['company_name'], $rs['id']).'">Company profile</a></li>
	<li><a href="/edit-company-profile">Edit company profile</a></li>
	<li><a href="/notification-settings">Notification settings</a></li>
	<li><a href="/my-subscription">My subscription</a></li>
	<li><a href="/payment-history">Payment history</a></li>
	<li><a href="/post-a-job">Post a job</a></li>
	<li><a href="/sign-out">Sign out</a></li>
	</ul>

	';

	}

	else{

	$account_area = '<p style="margin: 0px 0px 10px 0px;"><strong>Welcome '.ucfirst($rs['firstname']).'</strong></p>

	<ul id="account_links">
	<li><a href="/my-account">My account</a></li>
	<!--<li><a href="/buy-sms">Buy SMS</a> ('.$rs['sms_remaining'].' remaining)</li>-->
	<li><a href="/post-a-job">Post a job</a></li>
	<li><a href="/personal-details">Personal details</a></li>
	<li><a href="/notification-settings">Notification settings</a></li>
	<li><a href="/sign-out">Sign out</a></li>
	</ul>

	';

	}

}

else{

$account_header = 'Login';

$account_area = '
<a href="/register" class="top_right_link"><strong>Register FREE</strong></a>

<form method="post">

'.$login_result.'

<div class="rowWrapper">
<div class="rowLeft"><label for="login_email">Email address</label></div>
<div class="rowRight"><input type="text" name="email_address" id="login_email"></div>
</div>

<div class="rowWrapper">
<div class="rowLeft"><label for="login_password">Password</label></div>
<div class="rowRight"><input type="password" name="password" id="login_password"></div>
</div>

<div class="rowWrapper">
<div class="rowLeft">&nbsp;</div>
<div class="rowRight"><label for="login_remember">Remember me</label> <input type="checkbox" name="remember" value="1" id="login_remember"><input type="image" src="/images/login_button.png" alt="Login"><p><a href="/reset-password">Reset password</a></p></div>
</div>

</form>

';

}

// latest jobs

/*
//Change By Vyas Ishan 17 May 2013 Task2
$sql = "SELECT jobs.id, title, area, trade FROM jobs 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	WHERE status = 'Open' AND custom != '1' AND job_active = '1' ORDER BY date_added DESC LIMIT 0,3";
	*/
$sql = "SELECT jobs.id, title, area, trade ,date_added FROM jobs 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	WHERE status = 'Open' AND custom != '1' AND job_active = '1' ORDER BY date_added DESC LIMIT 0,5";	
	
	
	$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
	$rows = mysql_num_rows($query);

	$latest_jobs_content = '';

	$i = 1;

	while($rs=mysql_fetch_assoc($query)){

	$style_change = ($i == $rows) ? ' style="border-bottom-width: 0px;"' : '' ;

	$link = build_job_link($rs['trade'],$rs['area'],$rs['id']);

	$latest_jobs_content.='<li'.$style_change.'><a href="'.$link.'" title="'.ucfirst($rs['title']).' - '.$rs['area'].'">'.ucfirst($rs['title']).'</a><br /><strong>'.$rs['area'].'<br/>'.date('F m, d',strtotime($rs['date_added'])).'</strong></li>'."\r\n";

	$i++;

	}

// latest feedback
/*
//Change By Vyas Ishan 17 May 2013 Task2
$sql = "SELECT jobs.id as job_id, tradesman.id as tradesman_id, username, company_name, area, trade, review, rating, title 
	FROM reviews 
	LEFT JOIN jobs ON reviews.job_id = jobs.id 
	LEFT JOIN areas ON jobs.area_id = areas.id 
	LEFT JOIN trades ON jobs.trade_id = trades.id 
	LEFT JOIN tradesman ON reviews.tradesman_id = tradesman.id 
	WHERE rating >= 8 ORDER BY date_reviewed DESC LIMIT 0,1";
*/	
$sql = "SELECT jobs.id as job_id, tradesman.id as tradesman_id, username, company_name, area, trade, review, rating, title 
	FROM reviews 
	JOIN jobs ON reviews.job_id = jobs.id 
	JOIN areas ON jobs.area_id = areas.id 
	JOIN trades ON jobs.trade_id = trades.id 
	JOIN tradesman ON reviews.tradesman_id = tradesman.id 
	WHERE rating >= 8 
	ORDER BY RAND() 
	LIMIT 1";
$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
$rs=mysql_fetch_assoc($query);

$job_link = build_job_link($rs['trade'],$rs['area'],$rs['job_id']);
$tradesman_link = build_tradesman_link($rs['company_name'],$rs['tradesman_id']);

$latest_feedback_content = '
<p><a href="'.$tradesman_link.'" title="'.ucfirst(htmlentities($rs['company_name'])).'">'.ucfirst(htmlentities($rs['company_name'])).'</a></p>
<p>'.ucfirst(substr($rs['review'], 0, 100)).'</p>
<p><img src="/images/'.$rs['rating'].'-star.gif" alt="'.($rs['rating'] / 2).'/5" /></p>';

?>

</article>
	
	<div id="sidebar">
	
	<?php echo $rightColumn_content; ?>
	
	<?php echo $tradesmen_search; ?>
	
	<aside id="login">
	
		<h1><?php echo $account_header; ?></h1>
		
		<?php echo $account_area; ?>
	
	</aside>
	
	<aside id="latest_feedback" class="blue_box">
	
	<h1>Latest Feedback</h1>
	<?php echo $latest_feedback_content; ?>
	
	</aside>
	
	<aside id="latest_jobs" class="blue_box">
	
		<h1>Latest Jobs</h1>
		<a href="/view-jobs" class="top_right_link">See all jobs</a>
	
		<ul>
		<?php echo $latest_jobs_content; ?>
		</ul>
	
	</aside>
	
	<aside id="blog_articles" class="blue_box">

		<h1>Blog articles</h1>
		<a href="/blog" class="top_right_link">View the blog</a>
	
		<ul>
		<?php
		
		$sql = "SELECT article_title, article_url, article_date FROM blog ORDER BY article_date DESC LIMIT 0,5";
		$query=mysql_query( $sql ) or die ("Error in query: $sql . " . mysql_error());
		while($rs=mysql_fetch_assoc($query)){
		
		echo '<li><a href="/blog/'.$rs['article_date'].'/'.$rs['article_url'].'">'.$rs['article_title'].'</a></li>';
		
		}
	
		?>
		</ul>
	
	</aside>
	
	<?php echo get_ad('column'); ?>
	
	</div>
